package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping("/add")
    public R addDish(@RequestBody ShoppingCart shoppingCart) {
        //获取userId
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //判断是套餐还是菜品
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        if (null != dishId) {
            //说明是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //说明是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //判断是否已在购物车
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one != null) {
            //已在购物车,number+1
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
        //不在购物车,添加
        shoppingCart.setNumber(1);
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);

    }

    @GetMapping("/list")
    public R getList() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R remmoveDish(@RequestBody ShoppingCart shoppingCart) {
        //获取userId
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //判断是套餐还是菜品
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        if (null != dishId) {
            //说明是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //说明是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart oneDish = shoppingCartService.getOne(lqw);
        Integer number = oneDish.getNumber();
        if (number > 1) {
            oneDish.setNumber(number - 1);
            shoppingCartService.updateById(oneDish);
            return R.success(oneDish);
        }
        oneDish.setNumber(0);
        shoppingCartService.updateById(oneDish);
        shoppingCartService.removeById(oneDish.getId());
        return R.success(oneDish);
    }

    @DeleteMapping("/clean")
    public R clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(lqw);

        return R.success("清空购物车成功");
    }
}